package com.api.service;

import com.api.mapper.BaseMapper;
import com.api.utils.OrderUtils;
import com.api.utils.RollPage;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by PC on 2017/7/11.
 */
abstract public class BaseService {

    protected BaseMapper baseMapper;

    void setBaseMapper(BaseMapper baseMapper) {
        this.baseMapper = baseMapper;
    }

//    abstract Long addBasic(Object obj) throws Exception;
//
//    abstract void modifyBasic(Object obj) throws Exception;

//    abstract void delBasic(Object obj) throws Exception;
    public int insert(Object obj)
    {
        return baseMapper.insert(obj);
    }
    public int insertSelective(Object obj)
    {
        return baseMapper.insertSelective(obj);
    }
    public int updateByPrimaryKey(Object obj)
    {
        return baseMapper.updateByPrimaryKey(obj);
    }
    public int updateByPrimaryKeySelective(Object obj)
    {
        return baseMapper.updateByPrimaryKeySelective(obj);
    }
    public Object findObjByKey(int seq) {
        return baseMapper.selectByPrimaryKey(seq);
    }
    public Object selectOneByParams(Map<String, Object> params, OrderUtils order)  {
        return baseMapper.selectOneByParams(params, order.toString());
    }
    public Object selectOneByParams(Map<String, Object> params)  {
        return baseMapper.selectOneByParams(params);
    }

    public List findListByParams(Map<String, Object> params, OrderUtils order)  {
        return baseMapper.selectListByParams(params, null, null, order.toString());
    }


    public int findCountByParams(Map<String, Object> params) {
        return baseMapper.selectCountByParams(params);
    }

    public RollPage findListPageByParams(Map<String, Object> params, OrderUtils order, Integer pageNum, Integer pageSize)  {
        Integer recordSum = baseMapper.selectCountByParams(params);
        RollPage rollPage;
        if (pageSize > 0) {
            rollPage = new RollPage(recordSum, pageNum, pageSize);
        } else {
            rollPage = new RollPage(recordSum, pageNum);
        }
        Integer pageOffset = rollPage.getPageOffset();

        if (recordSum > 0) {
            rollPage.setRecordList(baseMapper.selectListByParams(params, pageOffset, pageSize, order.toString()));
        } else {
            rollPage.setRecordList(new ArrayList());
        }
        return rollPage;
    }
}
